#include<PostSelf.h>

PostSelf::PostSelf()
{
    //构造函数里需要关联信号
    m_socketClient = SocketClient::getInstance();

    //关联读数据包信号
    connect(m_socketClient, SIGNAL(dataZijianPacketReady(SocketPacket)),
            this, SLOT(dataZijianPacketReady(SocketPacket)));
}

PostSelf::~PostSelf()
{
}

//发送自检命令，给send方法一个参数（设备ID）例："0001"
void PostSelf::send(QString id)
{
    m_socketClient->sendZijianCmd(id);
}

void PostSelf::establishConnection(QString ip, int port)
{
    bool connected = m_socketClient->isConnected();
    if(!connected)
    {
        QHostAddress ip("192.168.226.1");
        int port = 1234;
        m_socketClient->connectTcpServer(ip, port);
    }

}

//read直接给你一个字符串，数据格式为   设备ID；实验ID（这里没有，显示0000）；故障具体字符串
void PostSelf::dataZijianPacketReady(SocketPacket packet)
{
    recive = m_socketClient->analysisData(packet);
    qDebug()<<recive;

}



//void PostSelf::fileDetect()
//{
//    struct stat buffer;
//    path = QCoreApplication::applicationDirPath();
//    std::string dbDir = path.toStdString();
//    dbDir = dbDir+"/bokun.db";

//    if(! (stat(dbDir.c_str(), &buffer) == 0))
//    {

//        sqlD = new SqliteDriver();
//        sqlD->setDBDir(path);
//        sqlD->createDB("bokun");
//        sqlD->openDB("bokun");

//        sqlD->queryExec("CREATE TABLE IF NOT EXISTS device (id INTEGER PRIMARY KEY AUTOINCREMENT, -- '设备id' name VARCHAR(20) NOT NULL, -- '设备名称' mac VARCHAR(20) NOT NULL, -- 'Mac地址' ip VARCHAR(15) NOT NULL, -- 'IP地址' port INTEGER NOT NULL, -- '端口号' serial_number VARCHAR(50) NOT NULL, -- '设备序列号' status TINYINT NOT NULL, -- '设备状态:0-正在运行 1-未连接 2-故障' create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')), -- '创建时间' update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime'))-- '更新时间');");
//        sqlD->queryExec("CREATE TABLE IF NOT EXISTS experiment ( experiment_id INTEGER PRIMARY KEY AUTOINCREMENT, -- '实验id' experiment_name VARCHAR(50) NOT NULL, -- '实验名称' user_name VARCHAR(50), -- '用户名称(可选)' coding VARCHAR(40), -- '编码(可选)' device_id INTEGER NOT NULL, -- '实验设备id' type VARCHAR(20) NOT NULL, -- '实验类型' remarks VARCHAR(500), -- '备注(可选)' create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')), -- '创建时间' update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime'))-- '更新时间');");
//        sqlD->queryExec("CREATE TABLE IF NOT EXISTS sample ( id INTEGER PRIMARY KEY AUTOINCREMENT, -- '样本id' name VARCHAR(50) NOT NULL, -- '样本名称' color_value VARCHAR(20) NOT NULL, -- '颜色值：RGB值转化成字符串' remarks VARCHAR(500), -- '备注(可选)' create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')), -- '创建时间' update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime'))-- '更新时间' );");
//        sqlD->queryExec("CREATE TABLE IF NOT EXISTS chromosome ( id INTEGER PRIMARY KEY AUTOINCREMENT, -- '染料id' name VARCHAR(50) NOT NULL, -- '染料名称' wave_length VARCHAR(20) NOT NULL, -- '波长' creator VARCHAR(50) NOT NULL, -- '创建者' type VARCHAR(30) NOT NULL, -- '染料类型' status TINYINT NOT NULL, -- '修改状态：0-默认 1-非默认' create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')), -- '创建时间' update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime'))-- '更新时间');");
//        sqlD->queryExec("CREATE TABLE IF NOT EXISTS procedure ( id INTEGER PRIMARY KEY AUTOINCREMENT, -- 'id' experiment_id INTEGER NOT NULL, -- '实验id' reation_volume TINYINT NOT NULL, -- '反应体积' stage_name VARCHAR(20) NOT NULL, -- '阶段名称' step_order TINYINT NOT NULL, -- '步骤次序' temperature FLOAT NOT NULL, -- '温度' rate FLOAT NOT NULL, -- '速率' duration FLOAT NOT NULL, -- '持续时间' is_fluorescence BOOLEAN NOT NULL, -- '是否开启荧光' cycle_number INTEGER, -- '循环次数' create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')), -- '创建时间' update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime'))-- '更新时间' );");
//    }

//    std::string xmlDir = path.toStdString();
//    xmlDir = xmlDir + "/config.xml";

//    if(! (stat(xmlDir.c_str(), &buffer) == 0))
//    {

//    }

//}

